﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using UnityEngine;

using ApplProtocol;

public class RoomManagement
{
    private static long refreshCnt = 0;

    private static Dictionary<string, Room> username2Room = new Dictionary<string, Room>();
    private static List<Room> roomList = new List<Room>();

    public static void UpdateRoomList(RoomSceneUI ui)
    {
        refreshCnt++;

        for (int i = 0; i < roomList.Count(); i++)
        {
            roomList[i].Close();
        }

        roomList.Clear();
        username2Room.Clear();

        int roomCount = int.Parse(PclObject.args[0]);
        for (int i = 0; i < roomCount; i++)
        {
            //Debug.Log(refreshCnt + " 房间：" + i);
            int j = i * 5;
            MemberShip[] members = {
                new MemberShip(PclObject.args[j + 1], bool.Parse(PclObject.args[j + 2])),
                new MemberShip(PclObject.args[j + 3], bool.Parse(PclObject.args[j + 4]))
            };

            roomList.Add(
                new Room(
                    members, int.Parse(PclObject.args[j + 5]),
                    ui.gridLayout.transform
                )
            );

            roomList.Last().Bind(ui);

            if (PclObject.args[j + 1] == Client.userName || PclObject.args[j + 3] == Client.userName)
            {
                Client.isLeft = PclObject.args[j + 1] == Client.userName;
                Debug.Log(Client.isLeft);
                Client.activeRoom = roomList.Last();
            }

            //if (PclObject.args[j + 1] != "null")
            //    username2Room[PclObject.args[j + 1]] = roomList.Last();
            //if (PclObject.args[j + 3] != "null")
            //    username2Room[PclObject.args[j + 3]] = roomList.Last();

            //if(Client.isInRoom)
            //    Client.activeRoom = username2Room[Client.userName];
        }
    }

    public static void Delete()
    {
    }
}